/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import com.aptech.bluepumpkin.Comment;
import com.aptech.bluepumpkin.CommentsList;
import com.aptech.bluepumpkin.Event;
import com.aptech.bluepumpkin.beans.CommentFacade;
import com.aptech.bluepumpkin.beans.CommentsListFacade;
import com.aptech.bluepumpkin.beans.EventFacade;
import com.aptech.bluepumpkin.beans.UsersFacade;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import tools.CommentDataModel;

/**
 *
 * @author Trong Nguyen
 */
@ManagedBean
@SessionScoped
public class CommentControl implements Serializable {

    @EJB
    private EventFacade eventFacade;
    @EJB
    private UsersFacade usersFacade;
    @EJB
    private CommentFacade commentFacade;
    @EJB
    private CommentsListFacade commentsListFacade;
    private CommentsList[] commentsLists;
    private CommentDataModel commentDataModel;
    private CommentsList selected;

    public void delete(CommentsList cl){
        selected = cl;
        Comment comment = selected.getComment();
        Event event = selected.getEvent();
        event.getCommentsListList().remove(selected);
        eventFacade.edit(event);
        commentsListFacade.remove(selected);
        commentFacade.remove(comment);
        
    }
    public void activate() {
        for (CommentsList com : commentsLists) {
            Comment comment = com.getComment();
            if (!comment.getPublished()) {
                comment.setPublished(Boolean.TRUE);
                commentFacade.edit(comment);
            }
        }
    }

    public void deactivate() {
        for (CommentsList com : commentsLists) {
            Comment comment = com.getComment();
            if (comment.getPublished()) {
                comment.setPublished(Boolean.FALSE);
                commentFacade.edit(comment);
            }
        }

    }

    public CommentDataModel buildAndGetCommentDataModel() {
        List<CommentsList> allComments = commentsListFacade.getAllComments();
        commentDataModel = new CommentDataModel(allComments);
        return commentDataModel;
    }

    public CommentsList getSelected() {
        return selected;
    }

    public void setSelected(CommentsList selected) {
        this.selected = selected;
    }

    public CommentDataModel getCommentDataModel() {
        return commentDataModel;
    }

    public void setCommentDataModel(CommentDataModel commentDataModel) {
        this.commentDataModel = commentDataModel;
    }

    public CommentFacade getCommentFacade() {
        return commentFacade;
    }

    public void setCommentFacade(CommentFacade commentFacade) {
        this.commentFacade = commentFacade;
    }

    public EventFacade getEventFacade() {
        return eventFacade;
    }

    public void setEventFacade(EventFacade eventFacade) {
        this.eventFacade = eventFacade;
    }

    public CommentsListFacade getCommentsListFacade() {
        return commentsListFacade;
    }

    public void setCommentsListFacade(CommentsListFacade commentsListFacade) {
        this.commentsListFacade = commentsListFacade;
    }

    public CommentsList[] getCommentsLists() {
        return commentsLists;
    }

    public void setCommentsLists(CommentsList[] commentsLists) {
        this.commentsLists = commentsLists;
    }

    public UsersFacade getUsersFacade() {
        return usersFacade;
    }

    public void setUsersFacade(UsersFacade usersFacade) {
        this.usersFacade = usersFacade;
    }
}
